<script setup>
import { reactive, ref, onMounted } from 'vue';
import { useAuthStore } from "@/stores/auth";
import { useRouter } from "vue-router";
import authHttp from "@/api/authHttp";
import { ElMessage } from 'element-plus'



const authStore = useAuthStore();
const router = useRouter();

let formRef = ref()
let isLoading = ref(false)

let form = reactive({
  email: '',
  password: '',
  captcha:''
});

let captcha = reactive({
  captcha_key: '',
  image_base64: '',
})

const rules = {
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度需在6~20之间！', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入6位验证码', trigger: 'blur' },
    { min: 6, max: 6, message: '请输入6位验证码', trigger: 'blur' }
  ]
};


const submitForm = () => {
  formRef.value.validate(async (valid, fields) => {
    if (valid) {
      try {
        isLoading.value = true
        console.log(captcha.captcha_key,form.captcha)
        let verifyData = await authHttp.verifyCaptcha(captcha.captcha_key,form.captcha)
        console.log(verifyData);
        

        let data = await authHttp.login(form.email, form.password);
        let token = data.token;
        let user = data.user;
        authStore.setUserToken(user, token);
        isLoading.value = false
        ElMessage.success('登录成功')
        // 跳转到首页
        router.push({ name: "frame" });
      } catch (detail) {
        // alert(detail);
        ElMessage.error(detail)
      }

    }
  })

}

const refreshCaptcha = async () => {
  const data = await authHttp.getCaptcha()
  console.log(data)
  captcha.image_base64 = data.image_base64
  captcha.captcha_key = data.captcha_key

};


onMounted(async () => {
  try {
    refreshCaptcha()
  } catch (detail) {
    ElMessage.error(detail)
  }
})


</script>

<template>
  <div class="login-background">
    <div class="login-container">
      <h2 class="login-title">OA管理系统</h2>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="0px">
        <el-form-item prop="email">
          <el-input v-model="form.email" placeholder="邮箱">
            <template #prepend>
              <el-icon>
                <Message />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="密码">
            <template #prepend>
              <el-icon>
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="captcha">
          <el-row :gutter="10">
            <el-col :span="15">
              <el-input v-model="form.captcha" placeholder="验证码">
                <template #prepend>
                  <el-icon>
                    <Ticket />
                  </el-icon>
                </template>
              </el-input>
            </el-col>
            <el-col :span="9">
              <div class="captcha-image" @click="refreshCaptcha">
                <img :src="captcha.image_base64" alt="验证码" />
              </div>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm"   :loading="isLoading" class="login-button">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>



<style scoped>
.login-background {
  background-color: rgba(173, 216, 230, 0.8);
  /* 淡蓝色背景，适当设置透明度 */
  backdrop-filter: blur(10px);
  /* 毛玻璃效果 */
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  /* 改为顶部对齐 */
  padding-top: 10%;
  /* 向上移动20% */
}

.login-container {
  width: 360px;
  /* 登录部分扩大20% */
  background: rgba(255, 255, 255, 0.9);
  /* 背景稍微透明的白色 */
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  /* 标题居中 */
  margin-bottom: 20px;
  /* 标题与表单的间距 */
}

.captcha-image {
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-button {
  width: 100%;
  /* 登录按钮宽度100% */
}

.icon-custom {
  font-size: 20px;
  /* 调整图标大小 */
}
</style>